package com.guddqs.monkey.shop.user.web;

import com.guddqs.base.BaseController;
import com.guddqs.base.MapBean;
import com.guddqs.monkey.shop.user.service.IUserService;
import com.guddqs.util.HttpUtils;
import com.guddqs.util.JsonUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author generator-wq
 * @date 2018/12/07 11:40:05
 */
@RestController
@RequestMapping("/api/shop")
@Api(description = "商品")
public class ShopController extends BaseController {

    @Resource
    private IUserService userServiceImpl;

    @PostMapping("/findShop")
    public MapBean findShop(String skuId) throws Exception {
        String result = HttpUtils.httpClientGet("https://wxapp.m.jd.com/kwxitem/ware/view.json", "wareId=" + skuId + "&ifShow=0&bizId=&brandId=&fromType=wxapp&timestamp=" + System.currentTimeMillis());
        if (result != null) {
            Map<String, Object> shop = JsonUtils.getMap(result);
            return success(shop);
        } else {
            return error("sku info null");
        }

    }

    /**
     * @param keyword 关键词
     * @param page    分页
     * @param sort    1.销量 5.新品 3.价格升序 2.价格降序
     */
    @PostMapping("/search")
    public MapBean search(String keyword, Integer page, Integer sort) throws Exception {
        String outputStr = "_format_=json&keyword=" + keyword + "&page=" + page + "&sort=" + sort;
        String result = HttpUtils.httpClientGet("https://wxapp.m.jd.com/kwxso/ware/searchList.action", "fromType=wxapp&timestamp=" + System.currentTimeMillis() + "&" + outputStr);
        Map<String, Object> shop = JsonUtils.getMap(result);
        return success(shop.get("valueKwx"));

    }

    @PostMapping("/findDetail")
    public MapBean findDetail(String skuId) throws Exception {
        String result = HttpUtils.httpClientGet("https://wxapp.m.jd.com/kwxitem/wxdetail/detail.json", "wareId=" + skuId + "&fromType=wxapp&timestamp=" + System.currentTimeMillis());
        if (result != null) {
            Map<String, Object> shop = JsonUtils.getMap(result);
            return success(shop);
        } else {
            return error("sku info null");
        }

    }


}